﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using IHISCETeim.Model;
using IHISCETeim.BLL;
using IHISCETeim.Library;
using FineUI;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json;
using System.Data;

namespace IHISCETeim.Web.AgentSys.SvrMgr
{
    public partial class ReportMTTR : AgentPageBase
    {

        protected override Permission[] PowerKey
        {
            get
            {
                return new Permission[] { Permission.查看 };
            }
        }
        protected override string DWZRef
        {
            get { return "ReportMTTR"; }
        }

        #region Page_Load

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadData();
            }
        }

        private void LoadData()
        {

            // 每页记录数
            Grid1.PageSize = PageSize;
            ddlGridPageSize.SelectedValue = PageSize.ToString();

            BindGrid();
        }


        protected string GetSQLWhere()
        {
            string SeaVal = ttbSearchMessage.Text;

            string wheresql = "";

            string sqlcommonFiled = "";
            if (!string.IsNullOrEmpty(SeaVal))
            {
                sqlcommonFiled = QueryOperatorsEnumHelper.ConvertCommonOperator("ClaimsNumber", QueryOperatorsEnum.包含, SeaVal);
                if (!string.IsNullOrEmpty(wheresql))
                {
                    wheresql += " or " + sqlcommonFiled;
                }
                else
                {
                    wheresql = sqlcommonFiled;
                }
            }
            if (!string.IsNullOrEmpty(SeaVal))
            {
                sqlcommonFiled = QueryOperatorsEnumHelper.ConvertCommonOperator("SerialNumber", QueryOperatorsEnum.包含, SeaVal);
                if (!string.IsNullOrEmpty(wheresql))
                {
                    wheresql += " or " + sqlcommonFiled;
                }
                else
                {
                    wheresql = sqlcommonFiled;
                }
            }
            if (!string.IsNullOrEmpty(SeaVal))
            {
                sqlcommonFiled = QueryOperatorsEnumHelper.ConvertCommonOperator("AgentName", QueryOperatorsEnum.包含, SeaVal);
                if (!string.IsNullOrEmpty(wheresql))
                {
                    wheresql += " or " + sqlcommonFiled;
                }
                else
                {
                    wheresql = sqlcommonFiled;
                }
            }

            //if (this.rblSearchSvrAct.SelectedValue == "my")
            //{
            //    sqlcommonFiled = QueryOperatorsEnumHelper.ConvertCommonOperator("SrvActUserId", QueryOperatorsEnum.等于, base.User.UserId.ToString());
            //    if (!string.IsNullOrEmpty(wheresql))
            //    {
            //        wheresql += " and " + sqlcommonFiled;
            //    }
            //    else
            //    {
            //        wheresql = sqlcommonFiled;
            //    }
            //}

            if (this.FromIssueDate.Text != "")
            {
                sqlcommonFiled = QueryOperatorsEnumHelper.ConvertCommonOperator("IssueDate", QueryOperatorsEnum.范围, this.FromIssueDate.Text + "|" + this.ToIssueDate.Text);
                if (!string.IsNullOrEmpty(wheresql))
                {
                    wheresql += " and " + sqlcommonFiled;
                }
                else
                {
                    wheresql = sqlcommonFiled;
                }
            }


            return wheresql;
        }

        private void BindGrid()
        {

            Grid1.DataSource = AgentQltyController.MTTRGetPagination(GetSQLWhere(), Grid1.PageIndex + 1, Grid1.PageSize, Grid1.SortField + " " + Grid1.SortDirection, ref TotalCount)[0];
            Grid1.RecordCount = TotalCount;
            Grid1.DataBind();

        }

        #endregion

        #region Events

        

        protected void Grid1_Sort(object sender, GridSortEventArgs e)
        {
            Grid1.SortDirection = e.SortDirection;
            Grid1.SortField = e.SortField;
            BindGrid();
        }

        protected void Grid1_PageIndexChange(object sender, GridPageEventArgs e)
        {
            Grid1.PageIndex = e.NewPageIndex;
            BindGrid();
        }


        protected void Grid1_RowCommand(object sender, GridCommandEventArgs e)
        {
            int QltyId = GetSelectedDataKeyID(Grid1);
            if (e.CommandName == "Delete")
            {
                // 在操作之前进行权限检查
                if (!HasPower(Permission.删除))
                {
                    CheckPowerFailWithAlert();
                    return;
                }
                //执行删除代码  

                if (AgentQltyController.GetByQltyId(QltyId).SrvActUserId == base.User.UserId)
                {
                    //删除操作
                    AgentQltyController.Delete(QltyId);
                    BindGrid();
                }
                else
                {
                    Alert.Show("对不起,此经销商的担当不属于你,你不能进行删除操作");
                    return;
                }
            }
        }

        protected void Window1_Close(object sender, EventArgs e)
        {
            BindGrid();
        }

        protected void ddlGridPageSize_SelectedIndexChanged(object sender, EventArgs e)
        {
            Grid1.PageSize = Convert.ToInt32(ddlGridPageSize.SelectedValue);
            BindGrid();
        }

        #endregion

        protected void Grid1_RowDataBound(object sender, GridRowEventArgs e)
        {

            DataRowView dr = e.DataItem as DataRowView;
            try
            {
                string ClaimStateVal = dr["ClaimState"].ToString();
                System.Web.UI.WebControls.HyperLink xx = Grid1.Rows[e.RowIndex].FindControl("hlClaimState") as System.Web.UI.WebControls.HyperLink;
                System.Web.UI.WebControls.HyperLink hlopenClaims = Grid1.Rows[e.RowIndex].FindControl("openClaims") as System.Web.UI.WebControls.HyperLink;
                System.Web.UI.WebControls.HyperLink execlClaims = Grid1.Rows[e.RowIndex].FindControl("execlClaims") as System.Web.UI.WebControls.HyperLink;
                if (ClaimStateVal == ClaimState.品情再确认.ToString())
                {
                    xx.ToolTip = dr["CancelReason"].ToString();
                }
                if (HasPower(new Permission[] { Permission.修改, Permission.查看 }))
                {
                    hlopenClaims.NavigateUrl = "javascript:openAgentQltySvrAct(" + dr["QltyId"] + ",'" + dr["ClaimsNumber"] + "')";
                }
                execlClaims.NavigateUrl = "javascript:openExecl(" + dr["QltyId"] + ")";
                //
                // CheckPowerFailWithLinkButtonField(Grid1, "btnView");

            }
            catch
            {

            }
        }

        protected void rblSearchSvrAct_SelectedIndexChanged(object sender, EventArgs e)
        {
            BindGrid();
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            if (FromIssueDate.Text == "" || ToIssueDate.Text == "")
            {
                FromIssueDate.Text = "";
                ToIssueDate.Text = "";
            }
            BindGrid();
        }

        protected void btnExecl_Click(object sender, EventArgs e)
        {
            // string url = this.ExportExcelWebUrl+"/AgentSys/AgentQlty.aspx?SearchMessage={0}&SearchSvrAct={1}&FromFaultDate={2}&ToFaultDate={3}";
            string url = this.ExportExcelWebUrl + "/AgentSys/ReportMTTR.aspx?q=" + HttpUtility.UrlEncode(GetSQLWhere());
            // url = string.Format(url, ttbSearchMessage.Text, rblSearchSvrAct.SelectedValue, FromFaultDate.Text, ToFaultDate.Text);

            PageContext.RegisterStartupScript("top.window.open('" + url + "','TOP','alwaysRaised=yes,z-look=yes,width=500,height=200,toolbar=no,location=no,directories=no,status=no,menubars=no,scrollbars=no,resizable=no')");

            //PageContext.Redirect(url, "_top");
        }

        protected void Grid1_PreRowDataBound(object sender, GridPreRowEventArgs e)
        {
            // FineUI.HyperLinkField execlField = Grid1.FindColumn("execl") as FineUI.HyperLinkField;

            // execlField.DataNavigateUrlFormatString = this.ExportExcelWebUrl + "/AgentSys/AgentQltyExeclSing.aspx?id={0}";
        }
    }
}